package cn.iocoder.yudao.module.member.service.userpackage;

import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import cn.iocoder.yudao.module.member.controller.admin.userpackage.vo.*;
import cn.iocoder.yudao.module.member.dal.dataobject.userpackage.UserPackageDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import cn.iocoder.yudao.module.member.dal.mysql.userpackage.UserPackageMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.member.enums.ErrorCodeConstants.*;

/**
 * 用户关联套餐 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class UserPackageServiceImpl implements UserPackageService {

    @Resource
    private UserPackageMapper userPackageMapper;

    @Override
    public Long createUserPackage(UserPackageSaveReqVO createReqVO) {
        // 插入
        UserPackageDO userPackage = BeanUtils.toBean(createReqVO, UserPackageDO.class);
        userPackageMapper.insert(userPackage);
        // 返回
        return userPackage.getId();
    }

    @Override
    public void updateUserPackage(UserPackageSaveReqVO updateReqVO) {
        // 校验存在
        validateUserPackageExists(updateReqVO.getId());
        // 更新
        UserPackageDO updateObj = BeanUtils.toBean(updateReqVO, UserPackageDO.class);
        userPackageMapper.updateById(updateObj);
    }

    @Override
    public void deleteUserPackage(Long id) {
        // 校验存在
        validateUserPackageExists(id);
        // 删除
        userPackageMapper.deleteById(id);
    }

    private void validateUserPackageExists(Long id) {
        if (userPackageMapper.selectById(id) == null) {
            throw exception(USER_PACKAGE_NOT_EXISTS);
        }
    }

    @Override
    public UserPackageDO getUserPackage(Long id) {
        return userPackageMapper.selectById(id);
    }

    @Override
    public PageResult<UserPackageDO> getUserPackagePage(UserPackagePageReqVO pageReqVO) {
        return userPackageMapper.selectPage(pageReqVO);
    }

}